package com.stox.controller;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.stox.dto.web.Response;
import com.stox.manager.WatchListManager;
import com.stox.model.Scrip;
import com.stox.model.Watchlist;

@RestController
@RequestMapping("/watchLists")
public class WatchListController{

	@Autowired
	private WatchListManager watchListManager;
	
	@ResponseBody
	@RequestMapping(method=RequestMethod.GET, produces={"application/json"})
	public  Response<Set<Watchlist>> findByParentId(@RequestParam(value="parentId", required=false) Long id){
		return new Response<>(watchListManager.findByParentId(id));
	}
	
	@RequestMapping(method=RequestMethod.POST, consumes={"application/json"})
	public Response<Long> save(@RequestBody Watchlist watchList){
		return new Response<>(watchListManager.save(watchList).getId());
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
	public Response<Void> delete(@PathVariable("id") Long id){
		watchListManager.delete(id);
		return new Response<Void>();
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET, params = {"name"})
	public Response<Void> rename(@PathVariable Long id, @RequestParam("name") String newName){
		watchListManager.rename(id, newName);
		return new Response<Void>();
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.GET, params={"oldWatchListId","newWatchListId"})
	public Response<Void> moveEntry(@PathVariable("id") Long id, @RequestParam("oldWatchListId") Long oldWatchListId, 
			@RequestParam("newWatchListId") Long newWatchListId){
		watchListManager.moveEntry(id, oldWatchListId, newWatchListId);
		return new Response<Void>();
	}
	
	@ResponseBody
	@RequestMapping(value="/{id}/entries", method=RequestMethod.GET, produces={"application/json"})
	public Response<Set<Scrip>> findEntriesByWatchListId(@PathVariable("id") Long id){
		return new Response<>(watchListManager.findEntriesByWatchListId(id));
	}
	
	@RequestMapping(value="/{id}/entries/{entryId}", method=RequestMethod.DELETE)
	public Response<Void> deleteEntry(@PathVariable("id") Long id, @PathVariable("entryId") Long entryId){
		watchListManager.deleteEntry(entryId, id);
		return new Response<Void>();
	}
	
	@RequestMapping(value="/entries/{id}", method=RequestMethod.POST, consumes={"application/json"})
	public Response<Void> addToWatchList(@RequestBody List<Long> entryIds, @PathVariable("id") Long watchListId){
		watchListManager.addToWatchList(entryIds, watchListId);
		return new Response<Void>();
	}

	@RequestMapping(value="/{id}/children", method=RequestMethod.DELETE)
	public Response<Void> deleteByParentId(@PathVariable("id") Long parentId){
		watchListManager.deleteByParentId(parentId);
		return new Response<Void>();
	}
	
	@RequestMapping(value="/{id}/entries", method=RequestMethod.DELETE)
	public Response<Void> deleteEntriesByParentId(@PathVariable("id") Long parentId){
		watchListManager.deleteEntriesByParentId(parentId);
		return new Response<Void>();
	}
	
	@RequestMapping(value="/{id}/everything", method=RequestMethod.DELETE)
	public Response<Void> clear(@PathVariable("id") Long id){
		watchListManager.clear(id);
		return new Response<Void>();
	}
	
}
